package com.iyundao.io.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.PartymemberService;
import com.iyundao.io.mapper.PartyMemberMapper;
import com.iyundao.io.mapper.PartyMemberOrganizationMapper;
import com.iyundao.io.model.PartyMember;
import com.iyundao.io.model.PartyMemberOrganization;
import com.iyundao.io.tools.PartymemberVo;
import com.iyundao.io.tools.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PartymemberServiceImpl implements PartymemberService {

    @Autowired
    private PartyMemberMapper partyMemberMapper;

    @Autowired
    private PartyMemberOrganizationMapper partyMemberOrganizationMapper;


    /**
     * 人员添加的批量操作
     * @param list
     * 人员信息
     * @param organizationid
     * 组织id
     * @return
     */
    @Override
    public ResponseData batchInsert(List<PartymemberVo> list, String organizationid) {

        if (list.equals("") || list == null || organizationid.equals("") || organizationid == null) {
            return new ResponseData(0, "传入数据，详细信息或组织id为空");
        } else {
            List<Object> list1 = null;
            for (PartymemberVo pVo : list) {
                try {
                    /**
                     * 循环添加人员
                     */
                    PartyMember partyMember = new PartyMember();
                    partyMember.setPartymemberaddress(pVo.getPartymemberaddress());
                    partyMember.setPartymemberbirthday(pVo.getPartymemberbirthday());
                    partyMember.setPartymemberjointime(pVo.getJointime());
                    partyMember.setPartymembername(pVo.getPartymembername());
                    partyMember.setPartymemberphone(pVo.getPartymemberphone());
                    partyMember.setPartymembersex(pVo.getPartymembersex());
                    int i = partyMemberMapper.insert(partyMember);
                    if (i > 0) {
                        /**
                         * 人员添加成功后，添加人员组织关系
                         */
                        PartyMemberOrganization partyMemberOrganization = new PartyMemberOrganization();
                        Integer id = partyMemberMapper.selectByPhone(pVo.getPartymemberphone()).getId();
                        partyMemberOrganization.setJointime(pVo.getJointime());
                        partyMemberOrganization.setOrganizationid(organizationid);
                        partyMemberOrganization.setPartymemberid(id);
                        partyMemberOrganizationMapper.insert(partyMemberOrganization);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    list1.add(pVo.getPartymembername());
                }
            }
            return new ResponseData(200, "失败信息", list1);

        }
    }

    @Override
    public ResponseData insert(PartymemberVo partymemberVo, String organizationid) {
        PartyMember partyMember = new PartyMember();
        partyMember.setPartymemberaddress(partymemberVo.getPartymemberaddress());
        partyMember.setPartymemberbirthday(partymemberVo.getPartymemberbirthday());
        partyMember.setPartymemberjointime(partymemberVo.getJointime());
        partyMember.setPartymembername(partymemberVo.getPartymembername());
        partyMember.setPartymemberphone(partymemberVo.getPartymemberphone());
        partyMember.setPartymembersex(partymemberVo.getPartymembersex());
        int i = partyMemberMapper.insert(partyMember);
        if (i > 0) {
            /**
             * 人员添加成功后，添加人员组织关系
             */
            PartyMemberOrganization partyMemberOrganization = new PartyMemberOrganization();
            Integer id = partyMemberMapper.selectByPhone(partymemberVo.getPartymemberphone()).getId();
            partyMemberOrganization.setJointime(partymemberVo.getJointime());
            partyMemberOrganization.setOrganizationid(organizationid);
            partyMemberOrganization.setPartymemberid(id);
            Integer i1 = partyMemberOrganizationMapper.insert(partyMemberOrganization);
            if (i1 > 0) {
                return new ResponseData(200, "添加成功");
            } else {
                return new ResponseData(0, "人员添加成功，组织绑定未成功");
            }
        } else {
            return new ResponseData(0, "人员添加、组织绑定失败");
        }
    }

    /**
     * 转出操作
     * @param id
     * 人员关联表id
     * @return
     */
    @Override
    public ResponseData rollOut(Integer id) {
        Date outtime = new Date();
        int i = partyMemberOrganizationMapper.rollOut(outtime, id);
        if (i > 0) {
            return new ResponseData(200, "成功");
        } else {
            return new ResponseData(0, "转出失败");
        }
    }

    @Override
    public ResponseData selectById(Integer id) {
        PartyMember partyMember = partyMemberMapper.selectById(id);
        return new ResponseData(200, "成功", partyMember);
    }

    @Override
    public ResponseData updatePartymember(PartyMember partyMember) {
        if (partyMember.equals("") || partyMember == null) {
            return new ResponseData(0, "传入参数为空");
        } else {
            int i = partyMemberMapper.updatePartymember(partyMember);
            if (i > 0) {
                return new ResponseData(200, "修改成功");
            } else {
                return new ResponseData(0, "修改失败");
            }
        }
    }

    /**
     * 根据组织id查询人员
     * @param id
     * @return
     */
    @Override
    public ResponseData selectByOrganizationId(String id) {
        List<PartyMember> list = partyMemberMapper.selectByOrganizationId(Integer.parseInt(id));
        return new ResponseData(200,"成功", list);
    }
}
